<template>
  <view class="follows-container">
    <view v-if="loading" class="loading">加载中...</view>
    <view v-else-if="follows.length === 0" class="empty">暂无关注</view>
    <view v-else>
      <view v-for="user in follows" :key="user.userId" class="follow-item" @click="goToUserTasks(user)">
        <image :src="user.avatar ? (baseUrl + user.avatar) : '/static/images/profile.jpg'" class="avatar" />
        <view class="info">
          <text class="nickname">{{ user.nickName || user.userName }}</text>
          <text class="signature">{{ user.signature || '' }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import config from '@/config/config.js'
export default {
  data() {
    return {
      follows: [],
      loading: true,
      baseUrl: config.baseUrl + (config.apiPrefix || '')
    }
  },
  onLoad() {
    console.log('follows页面加载')
    this.loadFollows()
  },
  methods: {
    async loadFollows() {
      console.log('开始请求关注列表')
      this.loading = true
      try {
        const token = uni.getStorageSync('token')
        const res = await new Promise((resolve, reject) => {
          uni.request({
            url: this.baseUrl + '/system/user/follows',
            method: 'GET',
            header: {
              'Authorization': token
            },
            success: resolve,
            fail: reject
          })
        })
        console.log('关注接口返回', res)
        if (res.data && res.data.code === 200) {
          this.follows = res.data.data || []
        } else {
          uni.showToast({ title: (res.data && res.data.msg) || '获取关注失败', icon: 'none' })
        }
      } catch (e) {
        console.error('请求关注接口异常', e)
        uni.showToast({ title: '网络错误', icon: 'none' })
      } finally {
        this.loading = false
      }
    },
    goToUserTasks(user) {
      if (!user.userId) {
        uni.showToast({ title: '用户ID无效', icon: 'none' })
        return
      }
      uni.navigateTo({
        url: `/pages/user/user-tasks?userId=${user.userId}&nickName=${encodeURIComponent(user.nickName || user.userName || '')}`
      })
    }
  }
}
</script>

<style>
.follows-container {
  padding: 30rpx;
}
.follow-item {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
  cursor: pointer;
}
.avatar {
  width: 100rpx;
  height: 100rpx;
  border-radius: 50rpx;
  margin-right: 20rpx;
}
.info {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.nickname {
  font-size: 32rpx;
  color: #333;
  font-weight: bold;
}
.signature {
  font-size: 24rpx;
  color: #999;
}
.loading, .empty {
  text-align: center;
  color: #999;
  font-size: 28rpx;
  margin-top: 100rpx;
}
</style> 